package com.mypg;

import com.service.Impl.UserServiceImpl;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class CommentHelper {

    private static UserService userService;

    private int comment_id;
    private int user_id;
    private String user_nickname;
    private int question_id;
    private String comment_des;
    private String comment_time;
    private int comment_value;

    public static UserService getUserService(){
        if(userService==null){
            String config="applicationcontext.xml";
            ApplicationContext ac=new ClassPathXmlApplicationContext(config);
            userService=(UserServiceImpl) ac.getBean("userService");
        }
        return userService;
    }


    public static CommentHelper changeCommentToCommentHelper(Comment comment){
        CommentHelper commentHelper=new CommentHelper();
        commentHelper.setComment_id(comment.getComment_id());
        commentHelper.setUser_id(comment.getUser_id());
        commentHelper.setQuestion_id(comment.getQuestion_id());
        commentHelper.setComment_des(comment.getComment_des());
        commentHelper.setComment_value(comment.getComment_value());

        String user_nickname_tool=CommentHelper.getUserService().selectUserByUser_id(comment.getUser_id()).getUser_nickname();
        commentHelper.setUser_nickname(user_nickname_tool);
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        commentHelper.setComment_time(df.format(comment.getComment_time().getTime()));

        return commentHelper;
    }
    public static List<CommentHelper> changeCommentListToCommentHelperList(List<Comment> commentList){
        List<CommentHelper> commentHelperList=new ArrayList<>();
        for(int i=0;i<commentList.size();i++){
            commentHelperList.add(CommentHelper.changeCommentToCommentHelper(commentList.get(i)));
        }
        return commentHelperList;
    }




    public CommentHelper(){
    }
    public CommentHelper(int comment_id, int user_id, String user_nickname, int question_id, String comment_des, String comment_time, int comment_value) {
        this.comment_id = comment_id;
        this.user_id = user_id;
        this.user_nickname = user_nickname;
        this.question_id = question_id;
        this.comment_des = comment_des;
        this.comment_time = comment_time;
        this.comment_value = comment_value;
    }

    public int getComment_id() {
        return comment_id;
    }

    public void setComment_id(int comment_id) {
        this.comment_id = comment_id;
    }

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUser_nickname() {
        return user_nickname;
    }

    public void setUser_nickname(String user_nickname) {
        this.user_nickname = user_nickname;
    }

    public int getQuestion_id() {
        return question_id;
    }

    public void setQuestion_id(int question_id) {
        this.question_id = question_id;
    }

    public String getComment_des() {
        return comment_des;
    }

    public void setComment_des(String comment_des) {
        this.comment_des = comment_des;
    }

    public String getComment_time() {
        return comment_time;
    }

    public void setComment_time(String comment_time) {
        this.comment_time = comment_time;
    }

    public int getComment_value() {
        return comment_value;
    }

    public void setComment_value(int comment_value) {
        this.comment_value = comment_value;
    }

    @Override
    public String toString() {
        return "CommentHelper{" +
                "comment_id=" + comment_id +
                ", user_id=" + user_id +
                ", user_nickname='" + user_nickname + '\'' +
                ", question_id=" + question_id +
                ", comment_des='" + comment_des + '\'' +
                ", comment_time='" + comment_time + '\'' +
                ", comment_value=" + comment_value +
                '}';
    }
}
